<template>
  <view>
    <view class="bg" v-if="waitShow"></view>
    <view class="no_data" v-if="reportShow && empty && !waitManagerShow">
      <image class="img_nodata" src="https://ainengli.hzjrsw.com/jkhx/no_physical.png"></image>
      <view class="text">我们需要根据您的体检报告</view>
      <view class="text">为您制定个性化健康管理方案</view>
      <view class="text green">请耐心等待体检报告，或预约体检</view>
      <view class="button" @tap="reservationCheckup">预约体检</view>
    </view>
    <view v-else-if="!reportShow && empty && !waitManagerShow" class="empty">
      <image class="img" src="@/static/image/im-empty.png" />
      <text class="text">开通健康管理会员才能使用此服务哦</text>
      <button class="btn" @tap="goMember">加入会员</button>
    </view>
    <view class="no_data" v-else-if="!reportShow && empty && waitManagerShow">
      <image class="img_nodata2" src="https://ainengli.hzjrsw.com/jkhx/no_risk.png"></image>
      <view class="text">请耐心等待，会尽快为您分配健康管理师</view>
    </view>
    <ConversationList v-else-if="!reportShow && !empty && !waitManagerShow"></ConversationList>
  </view>
</template>

<script lang="ts" setup>
import { onShow, onLoad } from '@dcloudio/uni-app'
import { queryAccount } from '../../api/index'
import { ref } from 'vue'
import conversation from '@/pages/im/pages/Conversation/index.vue'
import store from '@/store/index'
import ConversationList from './conversation-list/index.vue'
import { trackInit } from '../../utils/reporter'

import { setContactTabUnread, setTabUnread } from '../../utils/msg'
import { H5_HAS_LOGIN_KEY } from '../../utils/constants'
const waitShow = ref(true)
const empty = ref(true)
const reportShow = ref(false)
const waitManagerShow = ref(false)
const userInfo = ref({})

// onLoad(() => {
//   console.log(store)

//   queryAccount(store.state.userInfo.empi)
//     .then((res) => {
//       console.log(res)
//       empty.value = false
//     })
//     .catch((e) => {
//       console.log(e)
//     })
// })

trackInit('ConversationUIKit')

onShow(() => {
  uni.removeStorageSync('healthManageTab')
  reportShow.value = false
  waitManagerShow.value = false
  waitShow.value = true
  empty.value = true
  userInfo.value = uni.getStorageSync('userInfo')
    ? JSON.parse(uni.getStorageSync('userInfo')).patientInfo
    : {}
  if (userInfo.value && userInfo.value.isVip == true) {
    queryAccount(store.state.userInfo.patientInfo.empi)
      .then((res) => {
        if (res.data.errorCode == 'TO_ASSIGN_MNG') {
          waitManagerShow.value = true
          waitShow.value = false
        } else if (res.data.errorCode == 'SUCCESS') {
          const { accid, token, appId } = res.data
          store.dispatch('app/IM_CONNECT', {
            imAccid: accid,
            imToken: token,
            appId: appId
          })
          empty.value = false
          waitShow.value = false
        } else {
          waitShow.value = false
        }
      })
      .catch((e) => {
        console.log(e)
      })
    return false
  }
  if (userInfo.value && userInfo.value.reportState == false) {
    reportShow.value = true
    waitShow.value = false
  } else {
    if (empty.value) {
      queryAccount(store.state.userInfo.patientInfo.empi)
        .then((res) => {
          if (res.data.errorCode == 'TO_ASSIGN_MNG') {
            waitManagerShow.value = true
            waitShow.value = false
          } else if (res.data.errorCode == 'SUCCESS') {
            const { accid, token, appId } = res.data
            store.dispatch('app/IM_CONNECT', {
              imAccid: accid,
              imToken: token,
              appId: appId
            })
            empty.value = false
            waitShow.value = false
          } else {
            waitShow.value = false
          }
        })
        .catch((e) => {
          console.log(e)
        })
    } else {
      // 重置选中会话
      // @ts-ignore
      waitShow.value = false
      uni.$UIKitStore?.uiStore.selectSession('')
      setTabUnread()
      setContactTabUnread()
      // #ifdef H5
      uni.getStorage({
        key: H5_HAS_LOGIN_KEY,
        success: function (res) {
          if (!res.data) {
            setTimeout(() => {
              window.location.reload()
            }, 500)
            uni.setStorageSync(H5_HAS_LOGIN_KEY, true)
          }
        },
        fail: function (error) {
          uni.setStorageSync(H5_HAS_LOGIN_KEY, true)
        }
      })
      // #endif
    }
  }
})

const goMember = () => {
  uni.navigateTo({ url: '/packA/pages/buyMember/index?source=im' })
}
const reservationCheckup = () => {
  console.log('跳转第三方 预约体检  未提供第三方链接')
  //   uni.navigateTo({ url: '/pages/questionaire/enter' })
}
</script>

<style lang="scss" scoped>
@import '../styles/common.scss';
.bg {
  position: fixed;
  z-index: 1;
  background-color: #f3f7f8;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
page {
  height: 100vh;
  overflow: hidden;
}

.empty {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 142rpx;
  height: 100vh;
  background: #fff;
  .img {
    width: 356rpx;
    height: 276rpx;
  }
  .text {
    font-family:
      PingFang SC,
      PingFang SC;
    font-weight: 500;
    font-size: 32rpx;
    color: #222222;
    line-height: 58rpx;
    text-align: center;
    font-style: normal;
    text-transform: none;
    margin-top: 32rpx;
  }
  .btn {
    width: 558rpx;
    height: 88rpx;
    background: #00d1b6;
    border-radius: 44rpx 44rpx 44rpx 44rpx;
    font-family:
      PingFang SC,
      PingFang SC;
    font-weight: 600;
    font-size: 32rpx;
    color: #ffffff;
    line-height: 88rpx;
    text-align: center;
    margin-top: 48rpx;
  }
}
.no_data {
  overflow: hidden;
  text-align: center;
  height: 100vh;
  background: #fff;
  .img_nodata {
    margin-top: 142rpx;
    margin-bottom: 32rpx;
    width: 336rpx;
    height: 276rpx;
  }
  .img_nodata2 {
    margin-top: 202rpx;
    margin-bottom: 70rpx;
    width: 336rpx;
    height: 196rpx;
  }
  .text {
    margin: 0 auto;
    width: 488rpx;
    font-weight: 500;
    font-size: 32rpx;
    color: #222222;
    line-height: 58rpx;
  }
  .green {
    color: #00d1b6;
  }
  .button {
    margin: 48rpx auto;
    width: 558rpx;
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
    background: #00d1b6;
    border-radius: 44rpx;
    font-weight: 600;
    font-size: 32rpx;
    color: #ffffff;
  }
}
</style>
